import { getRoleList, addRole, getUserRoleList, updateRole, getSingleRole, deleteRole } from '@/api/role'
import { getToken } from '@/utils/auth'

const role = {
  state: {
    roleList: [],
    userListAll: [],
    singleItem: {},
    roleListTotalItem: '',
    roken: getToken() || ''
  },
  mutations: {
    SET_LIST: (state, data) => {
      state.roleList = data
    },
    SET_USER_LIST_ALL: (state, data) => {
      state.userListAll = data
    },
    SET_ROLE_LIST_TOTAL_ITEM: (state, data) => {
      state.roleListTotalItem = data
    },
    SET_SINGLE_ITEM: (state, data) => {
      state.singleItem = data
    }
  },
  actions: {
    GetRoleListALl: ({ commit, state }) => {
      return new Promise((resolve, reject) => {
        getUserRoleList(state.token).then(response => {
          const data = response.data
          if (data && data.length > 0) { // 验证返回的roles是否是一个非空数组
            commit('SET_USER_LIST_ALL', data)
          } else {
            reject('getUserRoleListFailed: list must be a non-null array !')
          }
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },

    GetRoleList: ({ commit, state }, params) => {
      return new Promise((resolve, reject) => {
        getRoleList(state.token, params).then(response => {
          const data = response.data
          if (data.rows && data.rows.length > 0) { // 验证返回的roles是否是一个非空数组
            commit('SET_LIST', data.rows)
            commit('SET_USER_LIST_TOTAL_ITEM', data.total)
          } else {
            reject('getRolelistFailed: list must be a non-null array !')
          }
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },

    AddRole: ({ state }, params) => {
      return new Promise((resolve, reject) => {
        addRole(state.token, params).then(res => {
          if (res.result === 'success') {
            resolve(res)
          } else {
            reject(res)
          }
        }).catch(err => {
          reject(err)
        })
      })
    },

    UpdateRole: ({ state }, params) => {
      return new Promise((resolve, reject) => {
        updateRole(state.token, params).then(res => {
          if (res.result === 'success') {
            resolve(res)
          } else {
            reject(res)
          }
        }).catch(err => {
          reject(err)
        })
      })
    },

    GetSingleRole: ({ commit, state }, params) => {
      return new Promise((resolve, reject) => {
        getSingleRole(state.token, params).then(res => {
          if (res.result === 'success') {
            commit('SET_SINGLE_ITEM', res.data)
            resolve(res)
          } else {
            reject(res)
          }
        }).catch(err => {
          reject(err)
        })
      })
    },

    DeleteRole: ({ state }, params) => {
      return new Promise((resolve, reject) => {
        deleteRole(state.token, params).then(res => {
          if (res.result === 'success') {
            resolve(res)
          } else {
            reject(res)
          }
        }).catch(err => {
          reject(err)
        })
      })
    }
  }
}

export default role
